package cl.fbd.widget_examples.web.model

import net.liftweb.widgets.trees.model._

import net.liftweb.widgets.flot.FlotSerie

/*
 * Business Model:
 *
 * the table displayed in treetable examples contains two kind of rows:
 *
 * - time-serie which indicates: 
 *   - the kind of measure (for example: wind speed, snow fall)
 *   - the place where the measure was taken (a name)
 * - a measure contains 
 *   - the date when the measure was taken,
 *   - the value registered
 *
 * a time-serie has many measure 
 */

case class TimeSerie (what : String, where : String, var values : List [Measure])
{
  def sum () : Double = {
    values.foldLeft (0.0) ( (x : Double, y : Measure) => x + y.value) 
  }
  
  def toFlotSerie : FlotSerie = {
    new FlotSerie () {
      override val label = Some (where) 
      override val data = {for (measure <- values) yield (0.0 + measure.when.getTime, measure.value)}
    }
  }
  
  override def toString () : String = {
    what + " / " + where
  }
}

case class Measure (when : java.util.Date, value : Double)
{
  override def toString () : String = {
    when + ": " + value
  }
}



object TimeSerieMeasureModel  {
  val nieveCaidaPortillo = 
                      Measure (new java.util.Date (2007 - 1900, 6, 30), 40.0) ::
                      Measure (new java.util.Date (2007 - 1900, 7, 31), 160.0) ::
                      Measure (new java.util.Date (2007 - 1900, 8, 31), 70.0) ::
                      Measure (new java.util.Date (2007 - 1900, 9, 31), 5.0) ::
                      Nil
                      
  val tsPortillo = TimeSerie ("Nieve caida", "Portillo", nieveCaidaPortillo)
     
  //
     
  val nieveCaidaFarellones = 
                      Measure (new java.util.Date (2007 - 1900, 6, 30), 56.0) ::
                      Measure (new java.util.Date (2007 - 1900, 7, 31), 206.0) ::
                      Measure (new java.util.Date (2007 - 1900, 8, 31), 100.0) ::
                      Measure (new java.util.Date (2007 - 1900, 9, 31), 2.0) ::
                      Nil
                      
  val tsFarellones = TimeSerie ("Nieve caida", "Farellones", nieveCaidaFarellones)
  
  //
     
  val nieveCaidaPucon = 
                      Measure (new java.util.Date (2007 - 1900, 5, 30), 40.0) ::
                      Measure (new java.util.Date (2007 - 1900, 6, 30), 80.0) ::
                      Measure (new java.util.Date (2007 - 1900, 7, 31), 206.0) ::
                      Measure (new java.util.Date (2007 - 1900, 8, 31), 90.0) ::
                      Measure (new java.util.Date (2007 - 1900, 9, 31), 20.0) ::
                      Nil
                      
  val tsPucon = TimeSerie ("Nieve caida", "Pucon", nieveCaidaPucon)
     
  /**
   * create a tree model: measure of snow fall in chillean's mountain
   *
   * the levels in the tree are
   * chile : root (TimeSerie)
   * - ski station  (TimeSerie)
   *   - measure 
   */
  
  def initModel () : TreeModel [TimeSerie, TimeSerie] = {
     // the root element
     val chileTS = TimeSerie ("Nieve caida", "Chile", Nil)
     
     // the TreeModel
     val portillo = createTreeModel (tsPortillo)
     val farellones = createTreeModel (tsFarellones)
     val pucon = createTreeModel (tsPucon)
     
     val chile = new TreeModel (chileTS, portillo :: farellones :: pucon :: Nil)
     
     chile
  }
  
  /**
   * convert business model to TreeModel
   */
   
  def createTreeModel (timeSerie : TimeSerie) : TreeModel [TimeSerie, Measure] = {
    val measures : List [TreeModel [Measure, AnyRef]] = 
      for (i <- timeSerie.values) yield (new TreeModel [Measure, AnyRef] (i))
    
    new TreeModel (timeSerie, measures)
  }
  
}

